//
//  ByViewClassificationScroll.m
//  itboye_shop_ios
//
//  Created by pro on 17/2/8.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewClassificationScroll.h"
#define SELECT_COLOR [UIColor colorWithR:207 g:231 b:130]

@implementation ByCellClassificationItem
{
    UILabel *_titleLabel;
    UIView  *_leftView;
    UILabel *_countLabel;
    UIView  *_lineView;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    _titleLabel = [[UILabel alloc] init];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.font = FONT(14);
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.numberOfLines = 0;
    [self.contentView addSubview:_titleLabel];
    
    _leftView = [[UIView alloc] init];
    [self.contentView addSubview:_leftView];
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [self.contentView addSubview:_lineView];
    
    
    _countLabel = [[UILabel alloc] init];
    _countLabel.textColor = [UIColor blackColor];
    _countLabel.font = FONT(14);
    _countLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_countLabel];
}

-(void)setCateModel:(ByModelShopCate *)cateModel{
    _cateModel = cateModel;
    
    CGFloat x,y,height,width;
    x = 0;
    y = 10;
    width = 5;
    height = 30;
    _leftView.frame = CGRectMake(x, y, width, height);
    
    width = [@"各地特色菜" getSizeOfString:14].width + 10;
    x = _leftView.right + 5;
    y = 15;
    height = 20;
    _titleLabel.frame = CGRectMake(x, y, width, height);
    _titleLabel.adjustsFontSizeToFitWidth = YES;
    
    x = _titleLabel.right + 10;
    width = [@"111" getSizeOfString:14].width;
    _countLabel.frame = CGRectMake(x, y, width, height);
    _countLabel.text = _cateModel.count;
    _titleLabel.text = _cateModel.name;
    _lineView.frame = CGRectMake(0, 49, self.width, 1);
}

- (void)setSelecting:(BOOL)selecting {
    
    _selecting = selecting;
    
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSArray *colorAry = [userDefault valueForKey:@"select_color"];
    UIColor *selectColor = [UIColor colorWithR:[colorAry[0] integerValue]  g:[colorAry[1] integerValue] b:[colorAry[2] integerValue]];
    NSLog(@"%@",colorAry);
    _leftView.hidden = _selecting ? NO : YES;
    _leftView.backgroundColor = selectColor;
    _titleLabel.textColor = _selecting ? selectColor:[UIColor blackColor];
    _countLabel.textColor = _selecting ? selectColor:[UIColor blackColor];

//    self.backgroundColor = _selecting ? [UIColor colorWithWhite:1 alpha:0.25]:HTB_BG_GRAY;
}

@end


#pragma mark - 主视图
@implementation ByViewClassificationScroll

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    
    if (self = [super initWithFrame:frame style:style]) {
        
        [self setupDefault];
    }
    return self;
}
// 创建表视图
- (void)setupDefault {
    
    _selectingIndex = 0;
    
    self.backgroundColor = [UIColor whiteColor];
    self.delegate = self;
    self.dataSource = self;
    self.tableFooterView = [UIView new];

}



#pragma mark - UITableViewDataSource UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.classModels.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellClassificationItem *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[ByCellClassificationItem alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.selecting = (indexPath.section == _selectingIndex);
    
    ByModelShopCate *model = self.classModels[indexPath.section];
    cell.cateModel = model;
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 5;
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 点击当前的
    if (_selectingIndex == indexPath.section) {
        return;
    }
    
    _selectingIndex = indexPath.section;
    
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    [self reloadData];
    
    if (self.selectBlock) {
        self.selectBlock(self.classModels[indexPath.section]);
    }
}



@end
